package com.payment.transactions.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.payment.transactions.mapper.PaymentTransactionMapper;
import com.payment.transactions.model.PaymentTransaction;
import com.payment.transactions.service.PaymentTransactionService;

@Service
@Transactional
public class PaymentTransactionServiceImpl implements PaymentTransactionService {

    @Autowired
    private PaymentTransactionMapper paymentTransactionMapper;

    @Override
    public PaymentTransaction save(PaymentTransaction transaction) {
        paymentTransactionMapper.insert(transaction);
        return transaction;
    }

    @Override
    public int saveAll(List<PaymentTransaction> transactions) {
        if (transactions != null && !transactions.isEmpty()) {
            return paymentTransactionMapper.insertBatch(transactions);
        }
        return 0;
    }

    @Override
    public Optional<PaymentTransaction> findById(Integer id) {
        PaymentTransaction transaction = paymentTransactionMapper.getById(id);
        return Optional.ofNullable(transaction);
    }

    @Override
    public List<PaymentTransaction> findAll() {
        return paymentTransactionMapper.getAll();
    }

    @Override
    public Map<String, Object> findAll(int page, int size, Map<String, Object> params) {
        // 参数验证，确保page大于等于1
        if (page < 1) {
            page = 1;
        }
        if (size < 1) {
            size = 10; // 默认每页10条
        }
        // 构建查询参数
        int offset = (page - 1) * size;
        params.put("offset", offset);
        params.put("pageSize", size);

        // 查询数据
        List<PaymentTransaction> list = paymentTransactionMapper.getByPage(params);
        Long total = paymentTransactionMapper.countByPage(params);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (total + size - 1) / size);

        return result;
    }

    @Override
    public List<PaymentTransaction> findByPaymentPlatform(Integer paymentPlatform) {
        Map<String, Object> params = new HashMap<>();
        params.put("paymentPlatform", paymentPlatform);
        return paymentTransactionMapper.getByCondition(params);
    }

    @Override
    public List<PaymentTransaction> findByIncomeExpense(Integer incomeExpense) {
        Map<String, Object> params = new HashMap<>();
        params.put("incomeExpense", incomeExpense);
        return paymentTransactionMapper.getByCondition(params);
    }

    @Override
    public List<PaymentTransaction> findByTransactionStatus(String transactionStatus) {
        Map<String, Object> params = new HashMap<>();
        params.put("transactionStatus", transactionStatus);
        return paymentTransactionMapper.getByCondition(params);
    }

    @Override
    public Optional<PaymentTransaction> findByTransactionOrderNumber(String transactionOrderNumber) {
        PaymentTransaction transaction = paymentTransactionMapper.findByTransactionOrderNumber(transactionOrderNumber);
        return Optional.ofNullable(transaction);
    }

    @Override
    public PaymentTransaction update(PaymentTransaction transaction) {
        if (transaction.getId() == null || paymentTransactionMapper.getById(transaction.getId()) == null) {
            throw new RuntimeException("交易记录不存在: " + transaction.getId());
        }
        paymentTransactionMapper.update(transaction);
        return transaction;
    }

    @Override
    public int deleteById(Integer id) {
        // 使用逻辑删除
        return paymentTransactionMapper.delete(id);
    }

    @Override
    public int deleteAllById(List<Integer> ids) {
        if (ids != null && !ids.isEmpty()) {
            // 使用逻辑删除
            return paymentTransactionMapper.deleteBatch(ids);
        }
        return 0;
    }

    @Override
    public List<PaymentTransaction> findByMerchantOrderNumber(String merchantOrderNumber) {
        return paymentTransactionMapper.findByMerchantOrderNumber(merchantOrderNumber);
    }

    @Override
    public List<PaymentTransaction> findByCondition(Map<String, Object> params) {
        return paymentTransactionMapper.getByCondition(params);
    }

    @Override
    public List<Map<String, Object>> countByPlatform(Map<String, Object> params) {
        return paymentTransactionMapper.countByPlatform(params);
    }

    @Override
    public List<Map<String, Object>> countByIncomeExpense(Map<String, Object> params) {
        return paymentTransactionMapper.countByIncomeExpense(params);
    }
    
    @Override
    public List<Map<String, Object>> countByTransactionPartner(Map<String, Object> params) {
        return paymentTransactionMapper.countByTransactionPartner(params);
    }
}